import axios from "axios";
import {useUserStore} from "@/stores/index.js";
// 请求拦截器
axios.interceptors.request.use(function(config){
    const userStore = useUserStore()
    // 附加请求参数
    // config.params['token'] = '123';
    if(userStore.isLogin){
        config.headers['Authorization'] = 'Bearer ' + userStore.getUser.token;
    }
    return config;
},function(err){
    return Promise.reject(err);
});
// 响应拦截器
axios.interceptors.response.use(function(resp){
    if(resp.status == '200'){
        return resp.data;
    }else{
        return resp;
    }
},function(err){
    return Promise.reject(err);
});
const BASE_URL = 'http://localhost:8090/'  // GateWay
function axiosGet(url,params){
    return axios.get(BASE_URL+url,{params:params})
}
function axiosPost(url,params){
    return axios.post(
        BASE_URL + url,
        params,
        {"headers":{"Content-Type":"application/x-www-form-urlencoded"}}
    )
}
function post(url,params){
    return axios.post(
        BASE_URL + url,
        params,
        {"headers":{"Content-Type":"application/json"}}
    )
}
export {axiosGet, axiosPost, post}